import librosa
import matplotlib.pyplot as plt
import numpy as np
from DMuse.artear2_paddle.utils import util
from pydub import AudioSegment
# from utils import util
from ssqueezepy import ssq_cwt

mp3_files = util.get_files(r'C:/CloudMusic', 'mp3')

for filename in mp3_files:
    audio = AudioSegment.from_file(filename, "mp3")
    '''audio_duration = len(audio) / 1000  # 获取待切割音频的时长，单位是毫秒
    cut_parameters = np.arange(1, audio_duration, 10)  # np.arange()函数第一个参数为起点，第二个参数为终点，第三个参数为步长（10秒）
    start_time = 0
    for t in cut_parameters:
        stop_time = int(t * 1000)  # pydub以毫秒为单位工作
        audio_chunk = audio[start_time:stop_time]  # 音频切割按开始时间到结束时间切割
        audio_chunk.export(r'C:/CloudMusic/' + '.'.join(os.path.basename(filename).split('.')[:-1]) + '-{}.wav'.format(int(t/2)),
                           format="wav")  # 保存音频文件，t/2只是为了计数，根据步长改变。步长为5就写t/5
        start_time = stop_time - 1000  # 开始时间变为结束时间前1s---------也就是叠加上一段音频末尾的4s
        print('finish')'''

    raw = np.array(audio[:2000].get_array_of_samples()).reshape((-1, 2))
    spec_0 = librosa.feature.chroma_stft(raw[:, 0].astype('float32'), 44100, hop_length=512, n_chroma=480)
    spec_2, *_ = ssq_cwt(raw[:, 0], fs=44100)

    plt.imshow(spec_0)
    plt.show()
    plt.imshow(np.abs(spec_2), aspect='auto', vmin=0, vmax=.2, cmap='turbo')
    plt.show()
    # sd.play(raw, samplerate=44100, blocking=True)

